package com.graducationdesign.springboot.controller.Admin;

import com.graducationdesign.springboot.dao.NewsInformationDao;
import com.graducationdesign.springboot.entities.Administrator.Admin;
import com.graducationdesign.springboot.entities.crawling.NewsInformation;
import com.graducationdesign.springboot.mapper.AdminMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.zhneg
 * @create 2019-10-28-17:39
 */
@Controller
public class AdminLoginController {

    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());
    @Autowired
    AdminMapper adminMapper;

    @Autowired
    NewsInformationDao newsInformationDao;

    @PostMapping("/loginadmin")
    public String login(RedirectAttributes redirectAttributes,HttpSession session, String loginUser, String password,Map<String,Object> map,NewsInformation newsInformation){


        //判断用户不为空， 且密码为123
        if(!StringUtils.isEmpty(loginUser)&&!StringUtils.isEmpty(password)){
            logger.info("页面中的密码："+password+"页面中的登录名："+loginUser);

           Admin admin= adminMapper.getLoginUser(loginUser);
//           Student student=studentMapper.getLoginUser(loginUser);
            if(admin==null){

                map.put("msg","用户名不存在");
                return "Administrator/main/login";
            }

           logger.info("数据库中的密码："+admin.getPassword()+",数据库中的登录名："+admin.getLoginUser());
            if (admin.getLoginUser().equals(loginUser)&&admin.getPassword().equals(password)) {
                logger.info("正确登陆名" + loginUser);
                session.setAttribute("loginUser", loginUser);
                //登陆成功
//        return "main/index";
                //重定向可以redirect：可以重定向到任何一个请求中，地址栏改变
                logger.info("招聘信息查询。。。"+newsInformation);
                List<NewsInformation> newsInformations=newsInformationDao.findAll() ;
                redirectAttributes.addFlashAttribute("newsInformation",newsInformations);
//                map.put("newsInformation",newsInformations);
                //解决重复刷新表单的问题 ....由于我们使用的是redirect，所以上面要使用redirectAttributes类解决
            return "redirect:/main.html";
//            return "Administrator/main/indexs";
            }

        }
        //登录失败
      map.put("msg","密码错误");
     return "Administrator/main/login";

    }

    @GetMapping("/main.html")
    public String mainPage(){
        System.out.println("解决重复提交表单的问题。。。");
        return "Administrator/main/indexs";
    }
    @GetMapping("/logoutadmin")
    public String logout(HttpSession session){

        //1：清空session中的用户信息
        session.removeAttribute("loginUser");

        //2：注销session
        session.invalidate();

        return "redirect:/indexa.html";

    }
}
